<?php if(!defined('SIDELOADED')) { echo 'Direct access is not allowed.'; }

class Database extends mysqli {
  
  private $prefix,
          $result;
  
  public function __construct() {
    global $config;
    
    // Get the connection info from the configuration file
    $host     = @$config['database']['host']     ?: 'localhost';
    $user     = @$config['database']['username'] ?: 'root';
    $password = @$config['database']['password'] ?: null;
    $database = @$config['database']['database'] ?: null;
    $port     = @$config['database']['port']     ?: 3306;
    $socket   = @$config['database']['socket']   ?: null;
    
    $this->prefix = @$config['database']['prefix'] ?: '';
    
    // Create the connection
    parent::connect($host, $user, $password, $database, $port, $socket);
    
  }
  
  /**
   * Return the prefix of the database tables.
   * @return string The prefix used for the database tables.
   */
  public function Prefix() {
    
    return @$this->prefix;
    
  }
  
  /**
   * Check wether you are connected to the database.
   * @return bool True if you are connected, false if the connection failed.
   */
  public function Connected() {

    return $this->connected;

  }
  
  /**
   * Run a MySQL query and return it's result.
   * @param string $query The query you want to run.
   * @return mixed The result of the query.
   */
  public function Query($query) {
    
    return parent::query($query);
    
  }
  
  /**
   * Escape a given string to make it safe for usage in a query.
   * @param string $string The string to escape.
   * @return string The escaped string.
   * @todo Fix the escape method.
   */
  public function Escape($string) {

    // Start by removing useless whitespace
    $string = trim($string);

    // Convert all HTML-characters to something a little more... safe
    #$string = htmlentities($string);

    // Escape the string for use in Database Queries
    if(!get_magic_quotes_gpc()) {

      $string = $this->real_escape_string($string);

    }

    return $string;

  }
  
  /**
   * Return the ID of the last inserted query.
   * @return int The ID.
   */
  public function Id() {

    return $this->insert_id;

  }
  
  /**
   * Return the amount of records in $table.
   * @param string $table The name of the table.
   * @return int The amount of records.
   */
  public function Count($table) {
    
    $query  = 'SELECT COUNT(*) FROM `' . $table . '`;';
    $result = parent::query($query);
    $result = mysqli_fetch_object($result);
    
    return $result->{'COUNT(*)'};
    
  }
  
  /**
   * Inserts an object of $column => $value pairs into $table.
   * @param string $table The table to which the $object should be inserted.
   * @param array $object The object.
   * @return mixed The result of the query.
   */
  public function Insert($table, $object) {
    
    foreach($object as $column => $value) {
      
      $columns[] = '`' . $column . '`';
      $values[]  = '"' . $value . '"';
      
    }
    
    $query = 'INSERT INTO `' . $table . '`(' . implode(', ', $columns) . ') VALUES (' . implode(', ', $values) . ');';
    
    return parent::query($query);
    
  }
  
  /**
   * Run a MySQL query.
   * @param string $query The query you want to run.
   */
  public function Run($query) {
    
    $this->result = parent::query($query);
    
  }
  
  /**
   * Return an object from the latest query ran with Run().
   * @return type 
   */
  public function Result() {
    
    if(@$this->result->num_rows > 0) {
    
      return $this->result->fetch_object();
    
    } else {
      
      return false;
      
    }
    
  }
  
  /**
   * Return the amount of rows returned by the latest query ran with Run().
   * @return int The number of rows.
   */
  public function Rows() {
    
    return $this->result->num_rows > 0 ? $this->result->num_rows : -1;
    
  }
  
}
